from bot import Bot
from bots_common import *


class Bot_Smart_A(Bot):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.name = 'SmartA'

    def bot_give_card(self, msg):
        hand = msg.get('hand')
        hand.sort()
        rows = msg.get("board")
        weight=list(filter(lambda x: x < 51, hand))
        maxhand = max(hand)
        minhand = min(hand)
        lenhand = len(hand)
        betcard = maxhand
        rowends = list(map(lambda x: x[-1], rows))
        rowends.sort()
        maxrowend=rowends[-1]
        max2rowend=rowends[-2]
        max3rowend=rowends[-3]
        max4rowend=rowends[-4]
        notbiggest=list(filter(lambda x: x > maxrowend, hand))
        if notbiggest:
            betcard=min(notbiggest)
        else:
            notbiggest=list(filter(lambda x: x < maxrowend and x > max2rowend, hand))
            if notbiggest:
                betcard=min(notbiggest)
            else:
                notbiggest=list(filter(lambda x: x < max2rowend and x > max3rowend, hand))
                if notbiggest:
                    betcard=max(notbiggest)
                else:
                    notbiggest=list(filter(lambda x: x < max3rowend and x > max4rowend, hand))
                    if notbiggest:
                        betcard=max(notbiggest)

        msg = self.give_card(betcard)
        self.send_msg(msg)
        return betcard

    def bot_select_row(self, msg):
        print('about to select row')
        rows = msg.get("rows")
        index = lowest_penalty_row_index(rows)[0]
        answer = self.choose_row(index)
        self.send_msg(answer)
